import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()
protection= pd.read_csv(r'world protected forests procent.csv')
# format the numbers nicely
pd.options.display.float_format = '{:,.1f}'.format
pd.set_option('display.max_rows', None)
protection.head()
| Unnamed: 0 | total forest area 2020 1000 ha | % protected forest out fo total forest area | % forest of total land area | % all nature protected areas out of total surface | |
|---|---|---|---|---|---|
| 0 | Finland | 22409 | 16.7 | 73.7 | 13.3 |
| 1 | Sweden | 27980 | 8.1 | 68.7 | 14.5 |
| 2 | Japan | 17100 | 9.1 | 68.5 | 29.8 |
| 3 | Montenegro | 827 | 19.7 | 61.5 | 13.8 |
| 4 | Brazil | 519500 | 42.3 | 61.0 | 7.2 |
protection= protection.rename(columns= {'Unnamed: 0': 'Country'})
protection.iloc[41,2]= 1
protection= protection.rename(columns= {'% protected forest out fo total forest area': '% protected forest out of total forest area'})
protection= protection.rename(columns= {'% protected forest out of total forest area': '% Protected Forest'})
protection= protection.rename(columns= {'total forest area 2020 1000 ha': 'Forest Area 2020 (1000 ha)'})
protection= protection.rename(columns= {'% forest of total land area': '% Forest'})
protection= protection.rename(columns= {'% all nature protected areas out of total surface': '% Protected Nature Areas'})
import json
import pandas as pd
import plotly.graph_objects as go
import geopandas as gpd
europe_geojson = gpd.read_file("choropleth map protection\europe.geojson")
import os
file_size = os.path.getsize("choropleth map protection\europe.geojson")
file_size_MB = file_size / (1024 * 1024)
print(f"File size: {file_size_MB:.2f} MB")
file_size
File size: 4.58 MB
4804689
europe_geojson.head()
| featurecla | scalerank | labelrank | sovereignt | sov_a3 | adm0_dif | level | type | tlc | admin | ... | fclass_id | fclass_pl | fclass_gr | fclass_it | fclass_nl | fclass_se | fclass_bd | fclass_ua | filename | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Admin-0 country | 0 | 5 | Lithuania | LTU | 0 | 2 | Sovereign country | 1 | Lithuania | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | LTU.geojson | MULTIPOLYGON (((26.59453 55.66699, 26.60383 55... |
| 1 | Admin-0 country | 0 | 5 | Latvia | LVA | 0 | 2 | Sovereign country | 1 | Latvia | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | LVA.geojson | POLYGON ((27.35293 57.52760, 27.52817 57.52848... |
| 2 | Admin-0 country | 0 | 4 | Belarus | BLR | 0 | 2 | Sovereign country | 1 | Belarus | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | BLR.geojson | POLYGON ((23.60624 51.51740, 23.60231 51.53078... |
| 3 | Admin-0 country | 0 | 5 | Czechia | CZE | 0 | 2 | Sovereign country | 1 | Czechia | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | CZE.geojson | POLYGON ((14.81039 50.85845, 14.83168 50.85798... |
| 4 | Admin-0 country | 0 | 2 | Germany | DEU | 0 | 2 | Sovereign country | 1 | Germany | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | DEU.geojson | MULTIPOLYGON (((13.81572 48.76643, 13.78586 48... |
5 rows × 170 columns
protection['Country'].unique()
array(['Finland', 'Sweden', 'Japan', 'Montenegro', 'Brazil', 'Slovenia',
'Estonia', 'Latvia', 'Russian Federation', 'Austria', 'Belarus',
'Bosnia and Herzegovina', 'Liechtenstein', 'Georgia',
'Slovak Republic', 'Norway', 'Spain', 'Luxembourg', 'Portugal',
'United States', 'Bulgaria', 'Lithuania', 'Croatia',
'Czech Republic', 'Andorra', 'Italy', 'Germany', 'Switzerland',
'France', 'Serbia', 'Poland', 'Greece', 'Romania', 'Turkiye',
'Albania', 'Belgium', 'Hungary', 'Cyprus', 'Ukraine', 'Denmark',
'United Kingdom', 'Moldova', 'Ireland', 'Netherlands', 'Malta',
'Iceland', 'Monaco'], dtype=object)
protection['Country']= protection['Country'].str.replace('Czech Republic', 'Czechia')
protection['Country']= protection['Country'].str.replace('Bosnia and Herzegovina', 'Bosnia and Herz.')
protection['Country']= protection['Country'].str.replace('Turkiye', 'Turkey')
protection['Country']= protection['Country'].str.replace('Russian Federation', 'Russia')
protection['Country']= protection['Country'].str.replace('Slovak Republic', 'Slovakia')
protection['Country']= protection['Country'].str.replace('Russian Federation', 'Russia')
list(set(list(europe_geojson['name'].unique())) - set(list(protection['Country'])))
['Guernsey', 'North Macedonia', 'Kosovo', 'Jersey', 'Isle of Man', 'Åland', 'Faeroe Is.', 'San Marino', 'Gibraltar', 'Vatican']
list(set(list(protection['Country']))- set(list(europe_geojson['name'].unique())) )
['Cyprus', 'Brazil', 'Georgia', 'Turkey', 'United States', 'Japan']
import json
back_geojson = europe_geojson.to_json()
j = json.loads(back_geojson)
#fill in missing data
protection.loc[1, '% Protected Forest']= 6.8
# check countries which are in one file but not in the other
list(set(list(europe_geojson['name'].unique())) - set(list(protection['Country'])))
['Guernsey', 'North Macedonia', 'Kosovo', 'Jersey', 'Isle of Man', 'Åland', 'Faeroe Is.', 'San Marino', 'Gibraltar', 'Vatican']
protection.loc[47]= protection.loc[46]
protection.loc[48]= protection.loc[46]
protection.iloc[47, 0]= 'Kosovo'
protection.iloc[47, 1]= 1089
protection.iloc[47, 3]= 44.7
protection.iloc[47, 2]= 42.6
protection.iloc[47, 4]= 4.39
protection.iloc[48, 0]= 'North Macedonia'
protection.iloc[48, 1]= 989
protection.iloc[48, 3]= 38.5
protection.iloc[48, 2]= 13.1
protection.iloc[48, 4]= 8.9
# europe_geojson = gpd.read_file("choropleth map protection\custom.geojson")
import json
back_geojson = europe_geojson.to_json()
j = json.loads(back_geojson)
import pandas as pd
import plotly.graph_objects as go
# with open("file.geojson", "r", encoding="utf-8") as f:
# geometry = geojson.load(f)
import plotly.express as px
fig = px.choropleth_mapbox(protection, geojson=j, locations='Country', color='% Forest',featureidkey="properties.name",
color_continuous_scale= [[0, 'rgb(254, 125, 74)'], [0.2, 'yellow'],[0.4, 'rgb(221, 247, 180)'], [1, 'green']],
# range_color=(0, 12),
# scope="europe",
# projection="hammer",
labels={'% Forest':'% Forest out of All Land'},
mapbox_style="carto-positron",
zoom=3, center = {"lat": 58, "lon": 15},
opacity=0.7,
)
fig.update_layout(autosize=True,
width=900,
height=900,
margin=dict(
l=10,
r=10,
b=100,
t=100,
pad=1))
fig.update_layout(
title={
'text': "<b>% Forest out of Total Land",
'y':0.95,
'x':0.45,
'xanchor': 'center',
'yanchor': 'top'})
fig.show()